/**
Copyright [2008] [Julian Gamble] 

Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

	http://www.apache.org/licenses/LICENSE-2.0 
	
Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License. 
 **/
package aerith.swing;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.JPanel;

public class AerithContentPanel extends JPanel {
    ////////////////////////////////////////////////////////////////////////////
    // THEME SPECIFIC FIELDS
    ////////////////////////////////////////////////////////////////////////////
    
    /**
	 * 
	 */
	private static final long serialVersionUID = -5328162978807282100L;

	private GradientPaint backgroundGradient = new GradientPaint(
    		new Point(0,105), Color.decode("#202737"), 
    		new Point(0,432), Color.decode("#8590A5"));
    
    private BufferedImage light;
    
    private float lightOpacity = 0.5f;
    
    public AerithContentPanel() {
    	super();
    	loadImages();
    }
    
    /**
     * Create a new buffered JPanel with the specified layout manager
     *
     * @param layout  the LayoutManager to use
     */
    public AerithContentPanel(LayoutManager layout) {
        super(layout);
        loadImages();
    }

    private void loadImages() {
    	light = loadImage("/swing/content-light.png");
		
	}
    
    private BufferedImage loadImage(String resourcePath) {
    	java.net.URL imageurl = this.getClass().getResource(resourcePath);
    	BufferedImage result = null;
    	try {
			result =  javax.imageio.ImageIO.read(imageurl);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
    }

	@Override
    protected void paintComponent(Graphics g) {
        if (!isVisible()) {
            return;
        }
        
        Graphics2D g2 = (Graphics2D) g;
        
        Composite composite = g2.getComposite();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.setPaint(backgroundGradient);
        Rectangle bounds = g2.getClipBounds();
        g2.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                         lightOpacity));
        
        g2.drawImage(light, 0, 0, getWidth(), light.getHeight(), null);
        g2.setComposite(composite);
    }
}
